package com.ruoyi.legal.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.legal.domain.WorkDetail;
import com.ruoyi.legal.service.IWorkDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * 工作详情Controller
 * 
 * @author xwj
 * @date 2025-04-29
 */
@RestController
@RequestMapping("/legal/workDetail")
@Api(tags = "工作详情")
public class WorkDetailController extends BaseController {
    @Autowired
    private IWorkDetailService workDetailService;

    /**
     * 查询工作详情列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody WorkDetail workDetail) {
        startPage();
        List<WorkDetail> list = workDetailService.selectWorkDetailList(workDetail);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody WorkDetail workDetail) {
        startOrderBy();
        List<WorkDetail> list = workDetailService.selectWorkDetailList(workDetail);
        return success(list);
    }

    /**
     * 导出工作详情列表
     */
    @Log(title = "工作详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, WorkDetail workDetail) {
        List<WorkDetail> list = workDetailService.selectWorkDetailList(workDetail);
        ExcelUtil<WorkDetail> util = new ExcelUtil<WorkDetail>(WorkDetail.class);
        util.exportExcel(response, list, "工作详情数据");
    }

    /**
     * 获取工作详情详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id)  {
        return success(workDetailService.selectWorkDetailById(id));
    }

    /**
     * 新增工作详情
     */
    @Log(title = "工作详情", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody WorkDetail workDetail) {
        SysUser user = getLoginUser().getUser();
        if(workDetail.getId() == null) {
            workDetail.setCreateId(user.getUserId());
            workDetail.setCreateTime(DateUtils.getNowDate());
            workDetail.setCreateBy(user.getNickName());
            workDetailService.insertWorkDetail(workDetail);
        } else {
            workDetail.setUpdateId(user.getUserId());
            workDetail.setUpdateTime(DateUtils.getNowDate());
            workDetail.setUpdateBy(user.getNickName());
            workDetailService.updateWorkDetail(workDetail);
        }

        return success(workDetail);
    }

    /**
     * 修改工作详情
     */
    @Log(title = "工作详情", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody WorkDetail workDetail) {
        SysUser user = getLoginUser().getUser();
                workDetail.setUpdateId(user.getUserId());
                workDetail.setUpdateTime(DateUtils.getNowDate());
                workDetail.setUpdateBy(user.getNickName());
        return success(workDetailService.updateWorkDetail(workDetail));
    }

    /**
     * 删除工作详情
     */
    @Log(title = "工作详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(workDetailService.deleteWorkDetailByIds(ids));
    }

    @Log(title = "工作详情", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody WorkDetail workDetail) {
        int num = workDetailService.delete(workDetail);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<WorkDetail>> queryPage(@RequestBody WorkDetail workDetail) {
        Pages<WorkDetail> page = workDetailService.queryPage(workDetail);
        return ResponseModel.of(page);
    }
    @PostMapping("/countNum")
    @ApiOperation("统计数量")
    public ResponseModel countNum(@RequestBody WorkDetail workDetail) {
        List<Map> list = workDetailService.countNum(workDetail);
        return ResponseModel.of(list);
    }
}
